﻿//*** Guidance generated code ***//

using System;
using System.Linq;
using System.Web;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;

using Survey.Common;
using Survey.Web.Controls;

namespace Survey.Web
{
    public static partial class Ext
    {
        #region form on RazorControl<IViewModel>
        /// <summary>
        ///     Creates the starting FORM tag with:
        ///     1) Form 'action' attribute set to URL assembled from Model.MasterModel.ButtonSubmit
        ///     2) id="Str.WebControls.MainFormId" (can be evaluated by JavaScript)
        ///     3) enctype="multipart/form-data" allowing uploading files
        /// </summary>
        /// <returns></returns>
        public static HtmlString CreateForm<TModel>(this RazorControl<TModel> form)
            where TModel : class, IViewModel
        {
            return form.Model.MasterModel.ButtonSubmit.Is()
                       ? form.CreateForm(form.Model.MasterModel.ButtonSubmit)
                       : form.CreateForm(Str.WebControls.MainFormId);
        }

        public static HtmlString CreateForm<TModel>(this RazorControl<TModel> form, string buttonName)
            where TModel : class, IViewModel
        {
            var button = form.GetButton(buttonName);
            return button.Is() ? form.CreateForm(button) : new HtmlString(string.Empty);
        }

        public static IButtonItem GetButton<TModel>(this RazorControl<TModel> form, string buttonName)
            where TModel : class, IViewModel
        {
            return form.Model.MasterModel
                .ButtonsModel
                .Buttons
                .FirstOrDefault(b => b.ButtonName.IsEqual(buttonName));
        }

        /// <summary>
        ///     Renders the closing FORM tag if there ButtonSubmit.Is().
        ///     Also sets the button.IsFormTagClosed = true (to be not rendered twice)
        ///     Also appends the submit button inside this form element,
        ///     which allows on some smart browsers use the HTML instead of the JS
        /// </summary>
        /// <returns></returns>
        public static HtmlString CloseForm<TModel>(this RazorControl<TModel> form)
            where TModel : class, IViewModel
        {
            return form.Model.MasterModel.ButtonSubmit.Is()
                       ? form.CloseForm(form.Model.MasterModel.ButtonSubmit)
                       : form.CloseForm(Str.WebControls.MainFormId);
        }

        public static HtmlString CloseForm<TModel>(this RazorControl<TModel> form, string buttonName)
            where TModel : class, IViewModel
        {
            var button = form.Model.MasterModel
                .ButtonsModel
                .Buttons
                .FirstOrDefault(b => b.ButtonName.IsEqual(buttonName));
            return button.Is() ? form.CloseForm(button) : new HtmlString(string.Empty);
        }
        #endregion Form<IViewModel>
    }
}
